home *** CD-ROM | disk | FTP | other *** search
/ Ultra Pack / UltraComputing Partner Applications.iso / SunLabs / tclTK / src / tcl7.4 / tests / while.test < prev   
Encoding:
Text File  |  1994-12-18  |  2.7 KB  |  100 lines

  1. # Commands covered:  while
  2. #
  3. # This file contains a collection of tests for one or more of the Tcl
  4. # built-in commands.  Sourcing this file into Tcl runs the tests and
  5. # generates output for errors.  No output means no errors were found.
  6. #
  7. # Copyright (c) 1991-1993 The Regents of the University of California.
  8. # Copyright (c) 1994 Sun Microsystems, Inc.
  9. #
  10. # See the file "license.terms" for information on usage and redistribution
  11. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  12. #
  13. # @(#) while.test 1.8 94/12/17 16:20:33
  14.  
  15. if {[string compare test [info procs test]] == 1} then {source defs}
  16.  
  17. test while-1.1 {basic while loops} {
  18.     set count 0
  19.     while {$count < 10} {set count [expr $count+1]}
  20.     set count
  21. } 10
  22. test while-1.2 {basic while loops} {
  23.     set value xxx
  24.     while {2 > 3} {set value yyy}
  25.     set value
  26. } xxx
  27. test while-1.3 {basic while loops} {
  28.     set value 1
  29.     while {"true"} {
  30.     incr value;
  31.     if {$value > 5} {
  32.         break;
  33.     }
  34.     }
  35.     set value
  36. } 6
  37.  
  38. test while-2.1 {continue in while loop} {
  39.     set list {1 2 3 4 5}
  40.     set index 0
  41.     set result {}
  42.     while {$index < 5} {
  43.     if {$index == 2} {set index [expr $index+1]; continue}
  44.     set result [concat $result [lindex $list $index]]
  45.     set index [expr $index+1]
  46.     }
  47.     set result
  48. } {1 2 4 5}
  49.  
  50. test while-3.1 {break in while loop} {
  51.     set list {1 2 3 4 5}
  52.     set index 0
  53.     set result {}
  54.     while {$index < 5} {
  55.     if {$index == 3} break
  56.     set result [concat $result [lindex $list $index]]
  57.     set index [expr $index+1]
  58.     }
  59.     set result
  60. } {1 2 3}
  61.  
  62. test while-4.1 {errors in while loops} {
  63.     set err [catch {while} msg]
  64.     list $err $msg
  65. } {1 {wrong # args: should be "while test command"}}
  66. test while-4.2 {errors in while loops} {
  67.     set err [catch {while 1} msg]
  68.     list $err $msg
  69. } {1 {wrong # args: should be "while test command"}}
  70. test while-4.3 {errors in while loops} {
  71.     set err [catch {while 1 2 3} msg]
  72.     list $err $msg
  73. } {1 {wrong # args: should be "while test command"}}
  74. test while-4.4 {errors in while loops} {
  75.     set err [catch {while {"a"+"b"} {error "loop aborted"}} msg]
  76.     list $err $msg
  77. } {1 {can't use non-numeric string as operand of "+"}}
  78. test while-4.5 {errors in while loops} {
  79.     set x 1
  80.     set err [catch {while {$x} {set x foo}} msg]
  81.     list $err $msg
  82. } {1 {expected boolean value but got "foo"}}
  83. test while-4.6 {errors in while loops} {
  84.     set err [catch {while {1} {error "loop aborted"}} msg]
  85.     list $err $msg $errorInfo
  86. } {1 {loop aborted} {loop aborted
  87.     while executing
  88. "error "loop aborted""
  89.     ("while" body line 1)
  90.     invoked from within
  91. "while {1} {error "loop aborted"}"}}
  92.  
  93. test while-5.1 {while return result} {
  94.     while {0} {set a 400}
  95. } {}
  96. test while-5.2 {while return result} {
  97.     set x 1
  98.     while {$x} {set x 0}
  99. } {}
  100.